package org.jackysoft.struts2.fileserver;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Collection;
import java.util.Collections;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.User;
import org.jackysoft.entity.ViewFile;
import org.jackysoft.service.ViewFileService;
import org.jackysoft.struts2.workflow.AbstractWorkflowAction;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.IOUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Namespace("/fileflow/file")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class FileflowAction extends AbstractWorkflowAction<ViewFile> {
	
	private ViewFileService service;
	private ViewFile bean;
	private Collection<ViewFile> beans;
	private User user;
	private boolean uploaded=true;
	/**
	 * 
	 */
	private static final long serialVersionUID = 5045363268977542676L;

	// 下发工作流归档的文件
	public String save() throws Exception {
		
		service.dispatch(uploaded, bean, workflowId, upLoad, upLoadFileName);
		if(actionId!=-1){
			actionItem  =workflow.getActionItemById(actionId, workflowId, Collections.EMPTY_MAP);
			setting = entitySettingService.get(workflowId, actionId, actionItem.getStepId());
		}
		return "save";
	}

	public String stream() {
		bean = service.get(bean.getId());
		byte[] content = IOUtils.toByteArray(new File(bean.getRealPath()));
		setContentType(HttpUtil.extToMimeType(bean.getExtision()));
		setContentLength(bean.getSize());
		setUpLoadFileName(bean.getName()+"."+bean.getExtision());
		inputStream = new ByteArrayInputStream(content);
		return STREAM;
	}
	

	public String datalist() throws Exception {
		this.searchMap.put("admin", "liujinghua");
		this.searchMap.put("bean", bean);
		getJson();
		return JQGRID;
	}

	
	public String view() {
		bean = service.get(bean.getId());
		service.markReader(bean, user);
		return "view";
	}

	
	public String send() {
		bean = service.reSend(bean);
		return "send";
	}

	
	public String checkperm() throws Exception {
		
		Boolean cansend = service.canSendable(bean);
		bean = service.get(bean.getId());
		if(cansend){
			return "presend";
		}else
			return "checkperm";	
	}


	public String removes() throws Exception {
		service.remove(beans);
		return "removes";
	}
	

	@Resource
	public void setService(ViewFileService viewFileService) {
		this.service = viewFileService;
		this.setDataProvider(viewFileService);
	}

	public ViewFileService getService() {
		return service;
	}

	public ViewFile getBean() {
		return bean;
	}

	public void setBean(ViewFile bean) {
		this.bean = bean;
	}

	public Collection<ViewFile> getBeans() {
		return beans;
	}

	public void setBeans(Collection<ViewFile> beans) {
		this.beans = beans;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public boolean isUploaded() {
		return uploaded;
	}

	public void setUploaded(boolean uploaded) {
		this.uploaded = uploaded;
	}

}
